<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/2/5
 * Time: 22:05
 */

namespace app\api\controller;


use app\api\Base2Controller;

class Fujineng extends Base2Controller
{

    /**
     *添加技能
     */
    public function addjineng()
    {
        $userid = $this->user_id;
        if (!($brandid = input('brandid')) || !($catethree = input('catethree')))
        {
            return json_fail_return('请正确输入信息');
        }
        if(input('anzhuang') == 0){
            return json_fail_return('请选择安装技能');
        }
        if(input('weixiu') == 0){
            return json_fail_return('请选择维修技能');
        }
        if(input('baoyang') == 0){
            return json_fail_return('请选择保养技能');
        }
        $skil_id = input('id');

        $jineng = model('fujineng');
        #该用户下面的所有技能
        $where = [
            'fuwuid' => $userid,
        ];
        if(!empty($skil_id)){
            $where['id'] = $skil_id;
        }
        $jineng_info =  db('fuwu_personal_jineng')->where($where)->select();
      
        foreach ($jineng_info as $key => $value) {
            if($value['status'] != 3){
                if(strstr($value['brandid'],$brandid)){
                    return json_fail_return('该技能已存在');
                }
            }
            #如果所有技能里没有当前的品牌，直接新增
            if(!strstr($value['brandid'],$brandid)){
                break;
            }
        }
        $catrthree =$catethree;
        $cate2Arr = model('goodsclass')->getGoodsclassInfoById($catrthree);
        $catetwo =$cate2Arr['gc_parent_id'];
        $cate1Arr =  model('goodsclass')->getGoodsclassInfoById($catetwo);
        $cateone = $cate1Arr['gc_parent_id'];
        $data =[
            'cateone'=>$cateone,
            'catetwo'=>$catetwo,
            'catrthree'=>$catrthree,
            'brandid'=>','.$brandid.',',
            'anzhuang'=>input('anzhuang')?input('anzhuang'):0,
            'weixiu'=>input('weixiu')?input('weixiu'):0,
            'baoyang'=>input('baoyang')?input('baoyang'):0,
            'fuwuid'=>$userid,
            'status'=>1,
            'created_at'=>date('Y-m-d H:i:s',time()),
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
       
        if(!empty($skil_id)){
            $id = db('fuwu_personal_jineng')->where(['id'=>$skil_id])->update($data);
        }else{
            $id = $jineng->add($data);
        }
         
        if ($id){
            return json_success_return([],'提交成功,等待审核');
        }else{
            return json_fail_return('提交失败');
        }
    }

    /**
     * 技能列表
     */
    public function jinenglist()
    {
        $userid = $this->user_id;
        $model = model('fujineng');
        $pagesize = input('pagesize')?input('pagesize'):15;
        $page = input('page')?input('page'):1;
        $field='ds_fuwu_personal_jineng.id,ds_fuwu_personal_jineng.status,ds_fuwu_personal_jineng.fail,ds_fuwu_personal_jineng.brandid,ds_fuwu_personal_jineng.anzhuang,ds_fuwu_personal_jineng.weixiu,ds_fuwu_personal_jineng.baoyang,g.gc_name as cateonename,g2.gc_name as catetwoname,g3.gc_name as catethreename';
        $list  = $model->getList(['ds_fuwu_personal_jineng.fuwuid'=>$userid,'ds_fuwu_personal_jineng.status2'=>1],$field,$pagesize);
        foreach ($list as $key=>$value)
        {
            $ids = mb_substr(mb_substr($value['brandid'],1),0,-1);
            $brandArr = model('brand')->getBrandList(['brand_id'=>['in',$ids]],'brand_id,brand_name');
//            1待审核2审核成功3审核失败
            if ($value['status'] ==1) {
                $list[$key]['statusName'] = '待审核';
            }elseif($value['status'] == 2){
                $list[$key]['statusName'] = '审核成功';
            }elseif($value['status'] == 3){
                $list[$key]['statusName'] = '审核失败';
            }
            if ($value['anzhuang'] ==0) {
                $list[$key]['anzhuangName'] = '无';
            }elseif($value['anzhuang'] == 1){
                $list[$key]['anzhuangName'] = '了解';
            }elseif($value['anzhuang'] == 2){
                $list[$key]['anzhuangName'] = '熟练';
            }
            if ($value['weixiu'] ==0) {
                $list[$key]['weixiuName'] = '无';
            }elseif($value['weixiu'] == 1){
                $list[$key]['weixiuName'] = '了解';
            }elseif($value['weixiu'] == 2){
                $list[$key]['weixiuName'] = '熟练';
            }
            if ($value['baoyang'] ==0) {
                $list[$key]['baoyangName'] = '无';
            }elseif($value['baoyang'] == 1){
                $list[$key]['baoyangName'] = '了解';
            }elseif($value['baoyang'] == 2){
                $list[$key]['baoyangName'] = '熟练';
            }
            $list[$key]['brandArr'] =$brandArr;
        }
        return json_success_return($list);

    }

    /**
     * 技能详情
     */
    public function jinenginfo()
    {
        $userid = $this->user_id;
        $skil_id = input('id');
        $info = db('fuwu_personal_jineng')->where(['id'=>$skil_id,'fuwuid'=>$userid])->find();

        if(!empty($info['cateone'])){
            $class_info = db('goodsclass')->where(['gc_id'=>$info['cateone']])->field('gc_id,gc_name')->find();
            $info['cateone_name'] = $class_info['gc_name'];
        }
        if(!empty($info['catetwo'])){
            $class_info = db('goodsclass')->where(['gc_id'=>$info['catetwo']])->field('gc_id,gc_name')->find();
            $info['catetwo_name'] = $class_info['gc_name'];
        }
        if(!empty($info['catrthree'])){
            $class_info = db('goodsclass')->where(['gc_id'=>$info['catrthree']])->field('gc_id,gc_name')->find();
            $info['catrthree_name'] = $class_info['gc_name'];
        }
        return json_success_return($info);
    }
}